"""
Case Type   : insert插入空值
Case Name   : A库下查询pg_type_nonstrict_basic_value视图，合理报错
Create At   : 2024年6月12日
Owner       : zhanghuan96
Description :
    1、创建数据库
    2、A库下查询pg_type_nonstrict_basic_value视图
    3、清理环境
Expect      :
    1、成功
    2、后来变成
    3、成功
History     :
"""

import os
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Logger import Logger


class CompatibilityTest050(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.db_name = "db_0050"

    def test_atan(self):
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        text = '-----step1:创建数据库 expect:成功-----'
        self.log.info(text)
        create_cmd = f"drop database if exists {self.db_name};" \
            f"create database {self.db_name} dbcompatibility 'A';"
        msg = self.sh_primary.execut_db_sql(create_cmd)
        self.log.info(msg)
        self.assertIn('CREATE DATABASE', msg, '执行失败:' + text)

        text = '-----step2:A库下查询pg_type_nonstrict_basic_value视图;' \
               'expect:合理报错---'
        self.log.info(text)
        sql_cmd = f"select * from pg_type_nonstrict_basic_value where " \
            f"typename = 'timestamp';"
        msg = self.sh_primary.execut_db_sql(sql_cmd,
                                            dbname=self.db_name)
        self.log.info(msg)
        self.assertIn('ERROR:  relation "pg_type_nonstrict_basic_value" does '
                      'not exist', msg, '执行失败:' + text)

    def tearDown(self):
        text = '----step3:清理环境;expect:成功----'
        self.log.info(text)
        sql_cmd = f"drop database if exists {self.db_name};"
        msg = self.sh_primary.execut_db_sql(sql_cmd, dbname='postgres')
        self.log.info(msg)
        self.assertIn('DROP DATABASE', msg, '执行失败:' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
